package com.zego.wawaji.message.service;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.zego.wawaji.R;
import com.noble.library.observer.MsgMgr;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.wawaji_client.AppTinkerLike;

import java.util.ArrayList;
import java.util.List;

/**
 * 全局通知管理
 * Created by Su on 2018/2/9.
 */
public class MsgManager implements View.OnTouchListener, PObserver {
    private Context mContext;
    private MsgListener mFloatingViewListener; // 悬浮窗监听器
    private final WindowManager mWindowManager;
    private List<MsgView> mFloatingViewList;  // 悬浮窗集合


    public MsgManager(Context context, MsgListener floatingViewListener) {
        this.mContext = context;
        this.mFloatingViewListener = floatingViewListener;
        this.mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        this.mFloatingViewList = new ArrayList<>();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    /**
     * 添加悬浮窗
     */
    public void addFloatingView() {
        // 悬浮窗配置信息
        DisplayMetrics dm = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(dm);
        MsgManager.Configs configs = new MsgManager.Configs();
        configs.overMargin = -(int) (8 * dm.density);
        configs.floatingViewX = dm.widthPixels;
        configs.floatingViewY = (int) (8 * dm.density);

        // 创建悬浮窗
        MsgView floatingView = new MsgView(this.mContext, configs.floatingViewX, configs.floatingViewY);
        floatingView.setOnTouchListener(this);
        floatingView.setOverMargin(configs.overMargin);
        floatingView.setMoveDirection(configs.moveDirection);
        floatingView.setAnimateInitialMove(configs.animateInitialMove);  // 移动是否带动画

        // 悬浮窗视图组件
        View view = initFloatView();

        // 设置悬浮窗的大小
        FrameLayout.LayoutParams targetParams = new FrameLayout.LayoutParams(configs.floatingViewWidth, configs.floatingViewHeight);
        view.setLayoutParams(targetParams);
        floatingView.addView(view);

        // 添加悬浮窗到集合
        this.mFloatingViewList.add(floatingView);

        // 添加悬浮窗
        this.mWindowManager.addView(floatingView, floatingView.getWindowLayoutParams());
    }

    /**
     * 初始化悬浮窗视图组件
     */
    private View initFloatView() {
        View view = LayoutInflater.from(AppTinkerLike.getContext()).inflate(R.layout.msg_float_view, null, false);

        view.setOnClickListener(v -> {
            // 关闭悬浮窗
        });
        return view;
    }


    private void stopFloat() {
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
        }
    }

    /**
     * 移除悬浮窗
     *
     * @param floatingView
     */
    private void removeFloatingView(MsgView floatingView) {
        int matchIndex = this.mFloatingViewList.indexOf(floatingView);
        if (matchIndex != -1) {
            this.mWindowManager.removeViewImmediate(floatingView);
            this.mFloatingViewList.remove(matchIndex);
        }
        if (this.mFloatingViewList.isEmpty()) {
            if (this.mFloatingViewListener != null) {
                this.mFloatingViewListener.onFinishFloatView();
            }
        }
    }

    /**
     * 移除所有的悬浮窗
     */
    public void removeAllFloatingView() {
        MsgMgr.getInstance().detach(this);

        if (this.mFloatingViewList != null) {
            for (MsgView floatingView : mFloatingViewList) {
                this.mWindowManager.removeViewImmediate(floatingView);
            }
            this.mFloatingViewList.clear();
        }
    }

    /**
     * 悬浮窗的配置信息
     */
    public static class Configs {
        /**
         * 悬浮窗的x坐标
         */
        public int floatingViewX;

        /**
         * 悬浮窗的y坐标
         */
        public int floatingViewY;

        /**
         * 悬浮窗的宽度（单位：px）
         */
        public int floatingViewWidth;

        /**
         * 悬浮窗的高度（单位：px）
         */
        public int floatingViewHeight;

        /**
         * 悬浮窗边缘的外边距
         */
        public int overMargin;

        /**
         * 悬浮窗移动方向
         */
        @MsgView.MoveDirection
        public int moveDirection;

        /**
         * 悬浮窗移动时是否带动画
         */
        public boolean animateInitialMove;

        public Configs() {
            this.floatingViewX = MsgView.DEFAULT_X;
            this.floatingViewY = MsgView.DEFAULT_Y;
            this.floatingViewWidth = MsgView.DEFAULT_WIDTH;
            this.floatingViewHeight = MsgView.DEFAULT_HEIGHT;
            this.overMargin = 0;
            this.moveDirection = MsgView.MOVE_DIRECTION_DEFAULT;
            this.animateInitialMove = true;
        }
    }
}
